//
//  IndexViewController.m
//  FlowTest
//
//  Created by yan on 2020/11/24.
//

#import "IndexViewController.h"
#import "MJRefresh.h"

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

@interface IndexViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *array;

@property(strong,nonatomic)NSArray *nameList;
@property(strong,nonatomic)NSArray *timeList;
@property(strong,nonatomic)NSArray *allTypeList;
@property(strong,nonatomic)NSArray *severTypeList;
@property(strong,nonatomic)NSArray *deveiceTypeList;
@property(strong,nonatomic)NSArray *deveicePointList;
@end

@implementation IndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self subView];
    [self setMoreData];
    [self LoadData];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

#pragma mark 设置数据源

-(void)setMoreData{
    self.nameList = @[@"张三",@"李四",@"王五",@"张晓华",@"二麻子",@"三扁担"];
    self.timeList = @[@"2020-10-20 12:00",@"2020-11-20 12:00",@"2020-10-23 14:20",@"2020-10-22 14:00",@"2020-10-10 12:01",@"2020-11-20 12:00",];
    self.allTypeList = @[@(OrderTypeContact),@(OrderTypeHome),@(OrderTypeWaiting),@(OrderTypeTransport),@(OrderTypeAccept),@(OrderTypeHandle),@(OrderTypeUnAgree),@(OrderTypeReject),@(OrderTypeClose),@(OrderTypeAgree),@(OrderTypeConfirmed),@(OrderTypeComplete)];
    self.severTypeList = @[@(OrderTypeWaiting),@(OrderTypeTransport),@(OrderTypeAccept),@(OrderTypeHandle),@(OrderTypeUnAgree),@(OrderTypeReject),@(OrderTypeClose),@(OrderTypeAgree)];
    self.deveiceTypeList = @[@"空调",@"电视",@"洗衣机",@"电脑"];
    self.deveicePointList = @[@"中部",@"左边",@"右边",@"后面"];
    
}


-(NSMutableArray *)getModelData{
    NSMutableArray *modelList = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i ++) {
        DataModel *model = [[DataModel alloc] init];
        model.time = [self getRandomData:_timeList];
        model.name = [self getRandomData:_nameList];
        model.price = [self getPrice];
        model.isGuarantee = i%2 ? YES:NO;
        model.isMsg = i%2 ? YES:NO;
        DeviceModel *deviceModel = [[DeviceModel alloc]init];
        deviceModel.deviceName = [self getRandomData:_deveiceTypeList];
        deviceModel.deviceType = [self getRandomData:_deveiceTypeList];
        deviceModel.devicePosition = [self getRandomData:_deveicePointList];
        model.device = deviceModel;
        switch (_type) {
            case OrderListTypeAll:
                model.status = [self getType:_allTypeList];
                break;
            case OrderListTypeContact:
                model.status = OrderTypeContact;
                break;
            case OrderListTypeHome:
                model.status = OrderTypeHome;
                break;
            case OrderListTypeServeing:
                model.status = [self getType:_severTypeList];
                break;
            case OrderListTypeConfirmed:
                model.status = OrderTypeConfirmed;
                break;
            case OrderListTypeComplete:
                model.status = OrderTypeComplete;
                break;
            default:
                break;
        }
        [modelList addObject:model];
    }
    return modelList;
}


-(NSString *)getPrice{
    NSInteger price = random() % 1000;
    return [NSString stringWithFormat:@"%ld",price];
}


-(NSString *)getRandomData:(NSArray *)array{
    if (array.count == 0) {
        return @"";
    }
    NSInteger count = array.count;
    NSInteger index = random() % count;
    if (index < count) {
        return array[index];
    }
    return @"";
}

-(OrderType)getType:(NSArray *)array{
    if (array.count == 0) {
        return OrderTypeAll;
    }
    NSInteger count = array.count;
    NSInteger index = random() % count;
    if (index < count) {
        return [array[index] intValue];
    }
    return OrderTypeAll;
}



#pragma mark 请求数据
-(void)LoadData{
    WS(weakSelf);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.array = [self getModelData];
        [weakSelf.tableView reloadData];
    });
}

-(void)LoadPageData{
    WS(weakSelf);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.array addObjectsFromArray:[self getModelData]];
        [weakSelf.tableView reloadData];
    });
}




#pragma mark 添加views
-(void)subView{
    [self.tableView registerNib:[UINib nibWithNibName:@"ContactTableViewCell" bundle:nil] forCellReuseIdentifier:@"ContactTableViewCell"];
    [self.view addSubview:_tableView];
}


#pragma mark initViews

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 84) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100;
        WS(weakSelf);
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf LoadData];
        }];
        _tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            [weakSelf LoadPageData];
        }];
    }
    return _tableView;
}


#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContactTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ContactTableViewCell"];
    cell.model = _array[indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}


@end
